
package org.platform.tool.util;

import com.itextpdf.text.*;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.PdfPCell;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import org.apache.commons.io.FileUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;
//import com.lowagie.text.pdf.fonts;

/**
 * Created by mabb on 15-4-8.
 */
public class PDFHelper {
    /**
     * 创建文档
     * 
     * @param contextList
     * @param filePath
     */
    public static void createDocument(List<Element> contextList, String filePath) {
	try {
	    Document doc = new Document(PageSize.A3, 50, 50, 50, 50);
	    PdfWriter pdfWriter = PdfWriter.getInstance(doc, new FileOutputStream(filePath));
	    pdfWriter.setStrictImageSequence(true);
	    // PdfWriter.getInstance(doc, new FileOutputStream(filePath));

	    doc.addAuthor("故障诊断软件");
	    doc.open();
	    for (Element eContext : contextList) {
		try {
		    if (null == eContext) {
			continue;
		    }
		    doc.add(eContext);
		} catch (DocumentException e) {
		    continue;
		}
	    }

	    doc.close();
	} catch (FileNotFoundException e1) {
	    e1.printStackTrace();
	} catch (DocumentException e1) {
	    e1.printStackTrace();
	}

    }

    private static BaseFont getChineseFont() {

	try {
	    // BaseFont baseFontChinese;
	    // 方法一： 使用windows系统字体（TrueType）
	    // BaseFont baseFontChinese =
	    // BaseFont.createFont("C:/Windows/Fonts/SIMYOU.TTF",BaseFont.IDENTITY_H,
	    // BaseFont.NOT_EMBEDDED);
	    // //方法二： 使用iTextAsian.jar中的字体
	    // baseFontChinese =
	    // BaseFont.createFont("STSong-Light",BaseFont.IDENTITY_H,
	    // BaseFont.NOT_EMBEDDED);
	    // //方法三： 使用资源字体（ClassPath）
	    // baseFontChinese =
	    // BaseFont.createFont("/SIMYOU.TTF",BaseFont.IDENTITY_H,
	    // BaseFont.NOT_EMBEDDED);

	    // baseFontChinese =
	    // BaseFont.createFont("STSong-Light","UniGB-UCS2-H",BaseFont.NOT_EMBEDDED);
	    BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H", BaseFont.NOT_EMBEDDED);
	    // Font fontChinese = new Font(baseFontChinese, 12, Font.NORMAL);
	    return bf;
	} catch (DocumentException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}
	return null;
    }

    /**
     * 创建标题
     * 
     * @param titleMsg
     * @return
     */
    public static Element productMainTitleContent(String titleMsg) {

	// Font f = new Font();
	// Font f = GetChineseFont();
	BaseFont bf = getChineseFont();
	Font f = new Font(bf, 20, Font.NORMAL);
	f.setColor(BaseColor.BLACK);
	f.setSize(20);
	f.setStyle(Font.BOLD);

	Paragraph p = new Paragraph(titleMsg, f);
	// p.setFont(f);
	p.setAlignment(Element.ALIGN_CENTER);
	return p;
    }

    /**
     * 一级标题
     * 
     * @param titleMsg
     * @return
     */
    public static Element productFirstTitleContent(String titleMsg) {
	// Font f = new Font();
	BaseFont bf = getChineseFont();
	Font f = new Font(bf, 18, Font.NORMAL);
	f.setColor(BaseColor.BLACK);
	f.setSize(18);
	f.setStyle(Font.BOLD);

	Paragraph p = new Paragraph(titleMsg, f);
	// p.setFont(f);
	p.setAlignment(Element.ALIGN_LEFT);
	return p;
    }

    /**
     * 二级标题
     * 
     * @param titleMsg
     * @return
     */
    public static Element productSecondTitleContent(String titleMsg) {
	// Font f = new Font();
	BaseFont bf = getChineseFont();
	Font f = new Font(bf, 16, Font.NORMAL);
	f.setColor(BaseColor.BLACK);
	f.setSize(16);
	f.setStyle(Font.BOLD);

	Paragraph p = new Paragraph(titleMsg, f);
	// p.setFont(f);
	p.setAlignment(Element.ALIGN_LEFT);
	return p;
    }

    /**
     * 三级标题
     * 
     * @param titleMsg
     * @return
     */
    public static Element productThirdTitleContent(String titleMsg) {
	// Font f = new Font();
	BaseFont bf = getChineseFont();
	Font f = new Font(bf, 16, Font.NORMAL);
	f.setColor(BaseColor.BLACK);
	f.setSize(16);
	f.setStyle(Font.BOLD);

	Paragraph p = new Paragraph(titleMsg, f);
	// p.setFont(f);
	p.setAlignment(Element.ALIGN_LEFT);
	return p;
    }

    /**
     * 普通文本
     * 
     * @param titleMsg
     * @return
     */
    public static Element productTextContent(String titleMsg) {
	// Font f = new Font();
	BaseFont bf = getChineseFont();
	Font f = new Font(bf, 12, Font.NORMAL);
	f.setColor(BaseColor.BLACK);
	f.setSize(12);
	f.setStyle(Font.NORMAL);

	Paragraph p = new Paragraph(titleMsg, f);
	// p.setFont(f);
	p.setAlignment(Element.ALIGN_LEFT);
	p.setIndentationLeft(2);
	return p;
    }

    /**
     * 生成图片
     * 
     * @param picFilePath
     * @return
     */
    public static Element productPicture(String picFilePath) {
	File f = FileUtils.getFile(picFilePath);
	try {
	    Jpeg jpg = new Jpeg(f.toURI().toURL());
	    jpg.setAlignment(Element.ALIGN_CENTER);
	    return jpg;
	} catch (BadElementException e) {
	    e.printStackTrace();
	} catch (MalformedURLException e) {
	    e.printStackTrace();
	} catch (IOException e) {
	    e.printStackTrace();
	}
	return null;
    }

    /**
     * 表格
     * 
     * @param tcTitles
     * @param tcContents
     * @return
     */
    public static Element productExcelContents(String[] tcTitles, String[][] tcContents) {
	int colNum = tcTitles.length;
	PdfPTable table = new PdfPTable(colNum);

	table.setSpacingBefore(10f);// 130
	table.getDefaultCell().setBorder(2);// 0

	BaseFont bf = getChineseFont();
	Font fh = new Font(bf, 12, Font.BOLD);
	Font fb = new Font(bf, 12, Font.NORMAL);
	for (int i = 0; i < tcTitles.length; i++) {
	    String colTitle = tcTitles[i];
	    PdfPCell cell = new PdfPCell(new Paragraph(colTitle, fh));
	    // cell.setColspan(3);
	    table.addCell(cell);
	}

	for (int i = 0; i < tcContents.length; i++) {
	    String[] row = tcContents[i];
	    for (String rowText : row) {
		PdfPCell cell = new PdfPCell(new Paragraph(rowText, fb));
		table.addCell(cell);
	    }
	}
	return table;
    }

    /**
     * @param tcTitles
     * @param tcContents
     * @return
     */
    public static Element productExcelContents(String[] tcTitles, String[] tcContents) {
	int colNum = tcTitles.length;
	PdfPTable table = new PdfPTable(colNum);

	table.setSpacingBefore(10f);// 130
	table.getDefaultCell().setBorder(0);

	BaseFont bf = getChineseFont();
	Font fh = new Font(bf, 12, Font.BOLD);
	Font fb = new Font(bf, 12, Font.NORMAL);
	for (int i = 0; i < tcTitles.length; i++) {
	    String colTitle = tcTitles[i];
	    PdfPCell cell = new PdfPCell(new Paragraph(colTitle, fh));
	    table.addCell(cell);
	}

	for (int i = 0; i < tcContents.length; i++) {
	    String row = tcContents[i];
	    PdfPCell cell = new PdfPCell(new Paragraph(row, fb));
	    table.addCell(cell);
	}
	return table;

    }


}
